<?php
namespace console\controllers;

use common\bonus\BaseBonus;
use common\models\Balance;
use common\models\BalanceLog;
use common\models\GroupDetail;
use common\models\GroupWinning;
use common\models\Member;
use common\models\TradeCommodity;
use common\models\TradeOrder;
use common\utils\CommonFun;

/**
 *
 * 拼团定时任务实现
 * 2020/08/08
 * gonglei
 */

class  GroupController extends CoreController
{
    /*
     * 拼团后期处理
     * 每天22:00后 处理
     * */
    public function actionGroupDispose()
    {

        //查询出当天那些人中奖 那些人入围
        $where = 'group_time="' . date("Y-m-d",time()) . '" and status=1';
        $data = GroupDetail::getAll($where);

        $model = new GroupDetail();
        if (!$data){ CommonFun::writeLog('没有需要结算的活动'); exit();}

        /*未中奖人员处理数据*/
       $re =  $model->finalist($data);
        if ($re){
            echo true;
        }
    }



    /*检查是否需要自动签约的*/
    public function  actionWinningCommodity(){
        $where = " winning_time <=(NOW() - interval 24 hour) and is_order=2";
        $data  = GroupWinning::getAll($where);

        $tradeOrder = new TradeOrder;
        foreach ($data as $k=>$v)
        {
            $member = Member::getOne(['member_id'=>$v['member_id']],['member_id','invite_id','member_name']);
            if (!$member)continue;
                $arr['type']        = 3;
                $arr['goods_id']    = $v['commodity_id'];
                $arr['amount']      = $v['group_money'];
                $arr['period']      = 0;
//                //创建订单
                $orderId = $tradeOrder->addData($arr, $member);

                if (!$orderId) { CommonFun::writeLog($v['id'].'订单创建失败'); continue;}
                //增加合同钱包
                $wheres ="source_id=".$v['group_detail_id']." and member_id=".$member." and source_type=6 and is_await=1";
                $balanceLog  = BalanceLog::updateAll(['is_await'=>0],$wheres);

                $re = Balance::addBalance($v['member_id'], 3, $arr['amount'], 5, $orderId);
                if (isset($re['state']) && $re['state'] == false) {
                    CommonFun::writeLog($v['id'] . $re['msg']); continue;
                }

                /*修改中奖表数据*/
                $res = GroupWinning::updateAll(['is_order' => 1], 'id=' . $v['id']);
                if (!$res){
                    CommonFun::writeLog($v['id'] . '修改中奖表数据失败'); continue;
                }
                $trade  = TradeCommodity::getOne(['commodity_id'=>$v['commodity_id']]);

                //触发后续
                $params = array();
                $params['money']        = $arr['amount'];
                $params['member_id']    = $v['member_id'];
                $params['source_id']    = 1;
                $params['mch_id']       = 0;
                $params['trigger_type'] = 1;
                $params['desc']         = 'from 【' . $member['member_name'] . '】';
                $params['order_type']   = $arr['type'];
                $params['period']       = $arr['period'] ?? 0;
                $params['day_income']   = $trade['day_income'];
                $params['commodity_id'] = $arr['goods_id'];
                $params['zhitui_income'] = $trade['zhitui_income'];
                BaseBonus::bonusRun($params);
        }
        return true;
    }
}